home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / DirectPlay / Conferencer / frmNetwork.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  2001-10-08  |  34.6 KB  |  857 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "comdlg32.ocx"
  3. Begin VB.Form frmNetwork 
  4.    BorderStyle     =   3  'Fixed Dialog
  5.    Caption         =   "vbConferencer"
  6.    ClientHeight    =   4605
  7.    ClientLeft      =   45
  8.    ClientTop       =   330
  9.    ClientWidth     =   3930
  10.    Icon            =   "frmNetwork.frx":0000
  11.    LinkTopic       =   "Form1"
  12.    MaxButton       =   0   'False
  13.    MinButton       =   0   'False
  14.    ScaleHeight     =   4605
  15.    ScaleWidth      =   3930
  16.    StartUpPosition =   3  'Windows Default
  17.    Begin VB.Timer tmrVoice 
  18.       Enabled         =   0   'False
  19.       Interval        =   10
  20.       Left            =   6435
  21.       Top             =   975
  22.    End
  23.    Begin VB.CheckBox chkVoice 
  24.       Caption         =   "Enable Voice Chat"
  25.       Height          =   255
  26.       Left            =   1140
  27.       TabIndex        =   9
  28.       Top             =   3660
  29.       Value           =   1  'Checked
  30.       Width           =   1635
  31.    End
  32.    Begin MSComDlg.CommonDialog cdlSend 
  33.       Left            =   6360
  34.       Top             =   3180
  35.       _ExtentX        =   847
  36.       _ExtentY        =   847
  37.       _Version        =   393216
  38.       DialogTitle     =   "Send File"
  39.       Filter          =   "Any File |*.*"
  40.       Flags           =   4
  41.       InitDir         =   "C:\"
  42.    End
  43.    Begin VB.Timer tmrJoin 
  44.       Enabled         =   0   'False
  45.       Interval        =   50
  46.       Left            =   6420
  47.       Top             =   540
  48.    End
  49.    Begin VB.Timer tmrUpdate 
  50.       Enabled         =   0   'False
  51.       Interval        =   10
  52.       Left            =   6420
  53.       Top             =   60
  54.    End
  55.    Begin VB.TextBox txtCall 
  56.       Height          =   285
  57.       Left            =   60
  58.       TabIndex        =   0
  59.       Top             =   300
  60.       Width           =   2535
  61.    End
  62.    Begin VB.ListBox lstUsers 
  63.       Height          =   2595
  64.       Left            =   60
  65.       TabIndex        =   3
  66.       Top             =   1020
  67.       Width           =   3795
  68.    End
  69.    Begin VB.CommandButton cmdHangup 
  70.       Height          =   495
  71.       Left            =   3240
  72.       MaskColor       =   &H00FF0000&
  73.       Picture         =   "frmNetwork.frx":030A
  74.       Style           =   1  'Graphical
  75.       TabIndex        =   2
  76.       ToolTipText     =   "Hang up"
  77.       Top             =   120
  78.       UseMaskColor    =   -1  'True
  79.       Width           =   495
  80.    End
  81.    Begin VB.CommandButton cmdCall 
  82.       Default         =   -1  'True
  83.       Height          =   495
  84.       Left            =   2700
  85.       MaskColor       =   &H000000FF&
  86.       Picture         =   "frmNetwork.frx":0A0C
  87.       Style           =   1  'Graphical
  88.       TabIndex        =   1
  89.       ToolTipText     =   "Call a friend"
  90.       Top             =   120
  91.       UseMaskColor    =   -1  'True
  92.       Width           =   495
  93.    End
  94.    Begin VB.CommandButton cmdWhiteBoard 
  95.       Height          =   495
  96.       Left            =   2325
  97.       MaskColor       =   &H000000FF&
  98.       Picture         =   "frmNetwork.frx":110E
  99.       Style           =   1  'Graphical
  100.       TabIndex        =   6
  101.       ToolTipText     =   "Use the whiteboard"
  102.       Top             =   4020
  103.       UseMaskColor    =   -1  'True
  104.       Width           =   495
  105.    End
  106.    Begin VB.CommandButton cmdChat 
  107.       Height          =   495
  108.       Left            =   1125
  109.       MaskColor       =   &H000000FF&
  110.       Picture         =   "frmNetwork.frx":1A18
  111.       Style           =   1  'Graphical
  112.       TabIndex        =   4
  113.       ToolTipText     =   "Chat with someone"
  114.       Top             =   4020
  115.       UseMaskColor    =   -1  'True
  116.       Width           =   495
  117.    End
  118.    Begin VB.CommandButton cmdSendFile 
  119.       Height          =   495
  120.       Left            =   1725
  121.       MaskColor       =   &H000000FF&
  122.       Picture         =   "frmNetwork.frx":2322
  123.       Style           =   1  'Graphical
  124.       TabIndex        =   5
  125.       ToolTipText     =   "Transfer files to someone"
  126.       Top             =   4020
  127.       UseMaskColor    =   -1  'True
  128.       Width           =   495
  129.    End
  130.    Begin VB.Label Label1 
  131.       BackStyle       =   0  'Transparent
  132.       Caption         =   "Enter a name or IP to call"
  133.       Height          =   195
  134.       Index           =   1
  135.       Left            =   60
  136.       TabIndex        =   8
  137.       Top             =   60
  138.       Width           =   2475
  139.    End
  140.    Begin VB.Label Label1 
  141.       BackStyle       =   0  'Transparent
  142.       Caption         =   "Users currently in this session"
  143.       Height          =   315
  144.       Index           =   0
  145.       Left            =   60
  146.       TabIndex        =   7
  147.       Top             =   780
  148.       Width           =   3735
  149.    End
  150.    Begin VB.Menu mnuPopup 
  151.       Caption         =   "PopUp"
  152.       Visible         =   0   'False
  153.       Begin VB.Menu mnuExit 
  154.          Caption         =   "E&xit"
  155.       End
  156.    End
  157. Attribute VB_Name = "frmNetwork"
  158. Attribute VB_GlobalNameSpace = False
  159. Attribute VB_Creatable = False
  160. Attribute VB_PredeclaredId = True
  161. Attribute VB_Exposed = False
  162. Option Explicit
  163. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  164. '  Copyright (C) 1999-2001 Microsoft Corporation.  All Rights Reserved.
  165. '  File:       frmNetwork.frm
  166. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  167. Implements DirectPlay8Event
  168. Implements DirectPlayVoiceEvent8
  169. 'You can make bigger or smaller chunks here
  170. Private Const mlFileChunkSize As Long = 512
  171. 'Variables for file transfers
  172. Private moReceivedFiles As New Collection
  173. Private moSendFiles As New Collection
  174. Private mlSendUnique As Long
  175. 'Misc private variables
  176. Private moCallBack As DirectPlay8Event
  177. Private mfExit As Boolean
  178. Private mfTerminate As Boolean
  179. Private mlVoiceError As Long
  180. Private Sub chkVoice_Click()
  181.     If gfNoVoice Then Exit Sub 'Ignore this since voice chat isn't possible on this session
  182.     If chkVoice.Value = vbChecked Then
  183.         ConnectVoice Me
  184.     ElseIf chkVoice.Value = vbUnchecked Then
  185.         If Not (dvClient Is Nothing) Then dvClient.UnRegisterMessageHandler
  186.         If Not (dvClient Is Nothing) Then dvClient.Disconnect DVFLAGS_SYNC
  187.         Set dvClient = Nothing
  188.     End If
  189. End Sub
  190. Private Sub cmdCall_Click()
  191.     If txtCall.Text = vbNullString Then
  192.         MsgBox "You must type the name or address of the person you wish to call before I can make the call.", vbOKOnly Or vbInformation, "No callee"
  193.         Exit Sub
  194.     End If
  195.     Connect Me, txtCall.Text
  196. End Sub
  197. Private Sub cmdChat_Click()
  198.     If lstUsers.ListCount < 2 Then
  199.         MsgBox "You must have at least two people in the session before you can chat.", vbOKOnly Or vbInformation, "Not enough people"
  200.         Exit Sub
  201.     End If
  202.     If ChatWindow Is Nothing Then Set ChatWindow = New frmChat
  203.     ChatWindow.Show vbModeless
  204.     'Notify everyone
  205.     SendOpenChatWindowMessage
  206.     Set moCallBack = ChatWindow
  207. End Sub
  208. Private Sub cmdHangup_Click()
  209.     'Cleanup and quit
  210.     mfExit = True
  211.     Unload Me
  212. End Sub
  213. Private Sub cmdSendFile_Click()
  214.     Dim lMsg As Long, lOffset As Long
  215.     Dim oBuf() As Byte
  216.     If lstUsers.ListIndex < 0 Then
  217.         MsgBox "You must select someone to send a file to before sending one.", vbOKOnly Or vbInformation, "No selection"
  218.         Exit Sub
  219.     End If
  220.     If lstUsers.ListIndex < 1 Then
  221.         MsgBox "You must select someone other than yourself to send a file to before sending one.", vbOKOnly Or vbInformation, "No selection"
  222.         Exit Sub
  223.     End If
  224.     'Ok, we can send a file.. Let them pick one
  225.     cdlSend.FileName = vbNullString
  226.     On Error Resume Next
  227.     cdlSend.ShowOpen
  228.     If Err Or (cdlSend.FileName = vbNullString) Then Exit Sub 'They clicked cancel
  229.     cdlSend.InitDir = GetFolder(cdlSend.FileName)
  230.     'Otherwise start the file send
  231.     LockSendCollection
  232.     Dim f As frmProgress
  233.     Set f = New frmProgress
  234.     With f
  235.         .sFileName = cdlSend.FileName
  236.         .lDPlayID = lstUsers.ItemData(lstUsers.ListIndex)
  237.         mlSendUnique = mlSendUnique + 1
  238.         .FileUniqueID = mlSendUnique
  239.         'We need to send a 'Request' message first
  240.         lOffset = NewBuffer(oBuf)
  241.         lMsg = MsgSendFileRequest
  242.         AddDataToBuffer oBuf, lMsg, LenB(lMsg), lOffset
  243.         AddDataToBuffer oBuf, mlSendUnique, LenB(mlSendUnique), lOffset
  244.         AddStringToBuffer oBuf, StripFileName(cdlSend.FileName), lOffset
  245.         dpp.SendTo lstUsers.ItemData(lstUsers.ListIndex), oBuf, 0, DPNSEND_NOLOOPBACK Or DPNSEND_GUARANTEED
  246.     End With
  247.     moSendFiles.Add f
  248.     UnlockSendCollection
  249. End Sub
  250. Private Sub cmdWhiteBoard_Click()
  251.     If lstUsers.ListCount < 2 Then
  252.         MsgBox "You must have at least two people in the session before you can use the whiteboard.", vbOKOnly Or vbInformation, "Not enough people"
  253.         Exit Sub
  254.     End If
  255.     If WhiteBoardWindow Is Nothing Then Set WhiteBoardWindow = New frmWhiteBoard
  256.     WhiteBoardWindow.Show vbModeless
  257.     'Notify everyone
  258.     SendOpenWhiteBoardWindowMessage
  259.     Set moCallBack = WhiteBoardWindow
  260. End Sub
  261. Private Sub Form_Load()
  262.     'First start our server.  We need to be running a server in case
  263.     'someone tries to connect to us.
  264.     StartHosting Me
  265.     'Add ourselves to the listbox
  266.     lstUsers.AddItem gsUserName
  267.     lstUsers.ItemData(0) = glMyPlayerID
  268.     'Now put up our system tray icon
  269.     With sysIcon
  270.         .cbSize = LenB(sysIcon)
  271.         .hwnd = Me.hwnd
  272.         .uFlags = NIF_DOALL
  273.         .uCallbackMessage = WM_MOUSEMOVE
  274.         .hIcon = Me.Icon
  275.         .sTip = "vbConferencer" & vbNullChar
  276.     End With
  277.     Shell_NotifyIcon NIM_ADD, sysIcon
  278. End Sub
  279. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  280.     Dim ShellMsg As Long
  281.     ShellMsg = X / Screen.TwipsPerPixelX
  282.     Select Case ShellMsg
  283.     Case WM_LBUTTONDBLCLK
  284.         ShowMyForm
  285.     Case WM_RBUTTONUP
  286.         'Show the menu
  287.         'If gfStarted Then mnuStart.Enabled = False
  288.         PopupMenu mnuPopup, , , , mnuExit
  289.     End Select
  290. End Sub
  291. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  292.     If Not mfExit Then
  293.         Cancel = 1
  294.         Me.Hide
  295.     End If
  296. End Sub
  297. Private Sub Form_Unload(Cancel As Integer)
  298.     Dim f As Form
  299.     Dim lCount As Long
  300.     Me.Hide
  301.     Shell_NotifyIcon NIM_DELETE, sysIcon
  302.     Cleanup
  303.     For lCount = 1 To moSendFiles.Count 'Clear the collection
  304.         moSendFiles.Remove 1
  305.     Next
  306.     Set moSendFiles = Nothing
  307.     For lCount = 1 To moReceivedFiles.Count 'Clear the collection
  308.         moReceivedFiles.Remove 1
  309.     Next
  310.     Set moReceivedFiles = Nothing
  311.     For Each f In Forms
  312.         If Not (f Is Me) Then
  313.             Unload f
  314.             Set f = Nothing
  315.         End If
  316.     Next
  317.     DeleteCriticalSection goSendFile
  318.     DeleteCriticalSection goReceiveFile
  319.     End
  320. End Sub
  321. Private Sub mnuExit_Click()
  322.     mfExit = True
  323.     Unload Me
  324. End Sub
  325. Private Sub ShowMyForm()
  326.     Me.Visible = True
  327. End Sub
  328. Private Sub tmrJoin_Timer()
  329.     tmrJoin.Enabled = False
  330.     MsgBox "The person you are trying to reach did not accept your call.", vbOKOnly Or vbInformation, "Didn't accept"
  331.     StartHosting Me
  332. End Sub
  333. Public Sub UpdatePlayerList()
  334.     Dim lCount As Long, dpPeer As DPN_PLAYER_INFO
  335.     Dim lInner As Long, fFound As Boolean
  336.     Dim lTotal As Long
  337.     lTotal = dpp.GetCountPlayersAndGroups(DPNENUM_PLAYERS)
  338.     If lTotal > 1 Then
  339.         cmdHangup.Enabled = True
  340.         cmdCall.Enabled = False
  341.     End If
  342.     For lCount = 1 To lTotal
  343.         dpPeer = dpp.GetPeerInfo(dpp.GetPlayerOrGroup(lCount))
  344.         If (dpPeer.lPlayerFlags And DPNPLAYER_LOCAL) = DPNPLAYER_LOCAL Then
  345.             'Don't add me
  346.         Else
  347.             fFound = False
  348.             'Make sure they're not already added
  349.             For lInner = 0 To lstUsers.ListCount - 1
  350.                 If lstUsers.ItemData(lInner) = dpp.GetPlayerOrGroup(lCount) Then fFound = True
  351.             Next
  352.             If Not fFound Then
  353.                 'Go ahead and add them
  354.                 lstUsers.AddItem dpPeer.Name
  355.                 lstUsers.ItemData(lstUsers.ListCount - 1) = dpp.GetPlayerOrGroup(lCount)
  356.             End If
  357.         End If
  358.     Next
  359. End Sub
  360. Private Sub SendOpenWhiteBoardWindowMessage()
  361.     Dim lMsg As Long, lOffset As Long
  362.     Dim oBuf() As Byte
  363.     'Now let's send a message asking the host to accept our call
  364.     lOffset = NewBuffer(oBuf)
  365.     lMsg = MsgShowWhiteBoard
  366.     AddDataToBuffer oBuf, lMsg, LenB(lMsg), lOffset
  367.     dpp.SendTo DPNID_ALL_PLAYERS_GROUP, oBuf, 0, DPNSEND_NOLOOPBACK
  368. End Sub
  369. Private Sub SendOpenChatWindowMessage()
  370.     Dim lMsg As Long, lOffset As Long
  371.     Dim oBuf() As Byte
  372.     'Now let's send a message asking the host to accept our call
  373.     lOffset = NewBuffer(oBuf)
  374.     lMsg = MsgShowChat
  375.     AddDataToBuffer oBuf, lMsg, LenB(lMsg), lOffset
  376.     dpp.SendTo DPNID_ALL_PLAYERS_GROUP, oBuf, 0, DPNSEND_NOLOOPBACK
  377. End Sub
  378. Private Sub RemovePlayer(ByVal lPlayerID As Long)
  379.     Dim lCount As Long
  380.     'Remove anyone who has this player id
  381.     For lCount = 0 To lstUsers.ListCount - 1
  382.         If lstUsers.ItemData(lCount) = lPlayerID Then lstUsers.RemoveItem lCount
  383.     Next
  384.     If Not (ChatWindow Is Nothing) Then ChatWindow.LoadAllPlayers
  385.     'Let's see if there are any files being sent to this user
  386.     Dim f As frmProgress
  387.     LockSendCollection
  388.     For Each f In moSendFiles
  389.         If f.lDPlayID = lPlayerID Then
  390.             'Notify the user
  391.             MsgBox "Cancelled transfering file " & f.sFileName & " because the user quit."
  392.             'Yup, get rid of this file
  393.             EraseSendFile f.FileUniqueID
  394.         End If
  395.     Next
  396.     UnlockSendCollection
  397.     'Now look through the receive collection
  398.     LockReceiveCollection
  399.     For Each f In moReceivedFiles
  400.         If f.lDPlayID = lPlayerID Then
  401.             'Notify the user
  402.             MsgBox "Cancelled receiving file " & f.sFileName & " because the user quit."
  403.             'Yup, get rid of this file
  404.             EraseReceiveFile f.FileUniqueID
  405.         End If
  406.     Next
  407.     UnlockReceiveCollection
  408.     If lstUsers.ListCount <= 1 Then 'We are the only person left
  409.         cmdCall.Enabled = True
  410.         cmdHangup.Enabled = False
  411.     End If
  412. End Sub
  413. Private Function StripFileName(ByVal sFile As String) As String
  414.     'Get rid of the path to the file (Strip everything after the last \)
  415.     If InStr(sFile, "\") Then
  416.         StripFileName = Right$(sFile, Len(sFile) - InStrRev(sFile, "\"))
  417.     Else
  418.         StripFileName = sFile
  419.     End If
  420. End Function
  421. Private Sub SendNextFilePart(ByVal lUniqueID As Long)
  422.     Dim lNewMsg As Long, lNewOffSet As Long
  423.     Dim oBuf() As Byte, lChunkSize As Long
  424.     Dim oFile() As Byte, f As frmProgress
  425.     'First we need to find the correct file in our send list
  426.     LockSendCollection
  427.     Set f = GetSendProgressForm(lUniqueID)
  428.     With f
  429.         'Send this chunk
  430.         lNewOffSet = NewBuffer(oBuf)
  431.         lNewMsg = MsgSendFilePart
  432.         AddDataToBuffer oBuf, lNewMsg, LenB(lNewMsg), lNewOffSet
  433.         AddDataToBuffer oBuf, .FileUniqueID, SIZE_LONG, lNewOffSet
  434.         'Is this chunk bigger than the amount we will send?
  435.         If .lCurrentPos + mlFileChunkSize > .lFileSize Then
  436.             'First send the chunksize
  437.             lChunkSize = .lFileSize - .lCurrentPos
  438.         Else
  439.             lChunkSize = mlFileChunkSize
  440.         End If
  441.         AddDataToBuffer oBuf, lChunkSize, LenB(lChunkSize), lNewOffSet
  442.         ReDim oFile(1 To lChunkSize)
  443.         'Now read in a chunk that size
  444.         If .filNumber = 0 Then
  445.             .filNumber = FreeFile
  446.             Open .sFileName For Binary Access Read As #.filNumber
  447.         End If
  448.         Get #.filNumber, , oFile
  449.         AddDataToBuffer oBuf, oFile(1), lChunkSize, lNewOffSet
  450.         dpp.SendTo .lDPlayID, oBuf, 0, DPNSEND_NOLOOPBACK Or DPNSEND_GUARANTEED
  451.         .lCurrentPos = .lCurrentPos + lChunkSize
  452.         'Update our transfer window
  453.         .SetValue .lCurrentPos
  454.         If .lCurrentPos >= .lFileSize Then
  455.             Close #.filNumber
  456.             'Now get rid of this member of the array
  457.             EraseSendFile .FileUniqueID
  458.         End If
  459.     End With
  460.     UnlockSendCollection
  461. End Sub
  462. Public Sub EraseSendFile(ByVal lUnique As Long)
  463.     Dim lCount As Long, f As frmProgress
  464.     'First we need to find the correct file in our send list
  465.     LockSendCollection
  466.     For lCount = moSendFiles.Count To 1 Step -1
  467.         Set f = moSendFiles.Item(lCount)
  468.         If f.FileUniqueID = lUnique Then
  469.             moSendFiles.Remove lCount
  470.             Unload f
  471.             Set f = Nothing
  472.             Exit For
  473.         End If
  474.     Next
  475.     UnlockSendCollection
  476. End Sub
  477. Public Sub EraseReceiveFile(ByVal lUnique As Long)
  478.     Dim lCount As Long, f As frmProgress
  479.     'First we need to find the correct file in our send list
  480.     LockReceiveCollection
  481.     For lCount = moReceivedFiles.Count To 1 Step -1
  482.         Set f = moReceivedFiles.Item(lCount)
  483.         If f.FileUniqueID = lUnique Then
  484.             moReceivedFiles.Remove lCount
  485.             Unload f.RequestForm
  486.             Set f.RequestForm = Nothing
  487.             Unload f
  488.             Set f = Nothing
  489.             Exit For
  490.         End If
  491.     Next
  492.     UnlockReceiveCollection
  493. End Sub
  494. Private Function GetSendProgressForm(ByVal lUnique As Long) As frmProgress
  495.     Dim f As frmProgress
  496.     LockSendCollection
  497.     For Each f In moSendFiles
  498.         If f.FileUniqueID = lUnique Then
  499.             Set GetSendProgressForm = f
  500.             Exit For
  501.         End If
  502.     Next
  503.     UnlockSendCollection
  504. End Function
  505. Private Function GetReceiveProgressForm(ByVal lUnique As Long) As frmProgress
  506.     Dim f As frmProgress
  507.     LockReceiveCollection
  508.     For Each f In moReceivedFiles
  509.         If f.FileUniqueID = lUnique Then
  510.             Set GetReceiveProgressForm = f
  511.             Exit For
  512.         End If
  513.     Next
  514.     UnlockReceiveCollection
  515. End Function
  516. Private Function GetFolder(ByVal sFile As String) As String
  517.     Dim lCount As Long
  518.     For lCount = Len(sFile) To 1 Step -1
  519.         If Mid$(sFile, lCount, 1) = "\" Then
  520.             GetFolder = Left$(sFile, lCount)
  521.             Exit Function
  522.         End If
  523.     Next
  524.     GetFolder = vbNullString
  525. End Function
  526. Private Sub tmrUpdate_Timer()
  527.     tmrUpdate.Enabled = False
  528.     If Not mfTerminate Then
  529.         MsgBox "The person you are trying to reach is not available.", vbOKOnly Or vbInformation, "Unavailable"
  530.     End If
  531.     StartHosting Me
  532.     mfTerminate = False
  533. End Sub
  534. Private Sub tmrVoice_Timer()
  535.     tmrVoice.Enabled = False
  536.     MsgBox "Could not start DirectPlayVoice.  This sample will not have any voice capablities." & vbCrLf & "Error:" & CStr(mlVoiceError), vbOKOnly Or vbInformation, "No Voice"
  537.     gfNoVoice = True
  538.     chkVoice.Value = vbUnchecked
  539.     chkVoice.Enabled = False
  540. End Sub
  541. 'We will hold a critical section for the two separate collections
  542. 'This will ensure that two threads can't access the data at the same time
  543. Public Sub LockSendCollection()
  544.     EnterCriticalSection goSendFile
  545. End Sub
  546. Public Sub UnlockSendCollection()
  547.     LeaveCriticalSection goSendFile
  548. End Sub
  549. Public Sub LockReceiveCollection()
  550.     EnterCriticalSection goReceiveFile
  551. End Sub
  552. Public Sub UnlockReceiveCollection()
  553.     LeaveCriticalSection goReceiveFile
  554. End Sub
  555. 'We will handle all of the msgs here, and report them all back to the callback sub
  556. 'in case the caller cares what's going on
  557. Private Sub DirectPlay8Event_AddRemovePlayerGroup(ByVal lMsgID As Long, ByVal lPlayerID As Long, ByVal lGroupID As Long, fRejectMsg As Boolean)
  558.     'VB requires that we must implement *every* member of this interface
  559.     If (Not moCallBack Is Nothing) Then moCallBack.AddRemovePlayerGroup lMsgID, lPlayerID, lGroupID, fRejectMsg
  560. End Sub
  561. Private Sub DirectPlay8Event_AppDesc(fRejectMsg As Boolean)
  562.     'VB requires that we must implement *every* member of this interface
  563.     If (Not moCallBack Is Nothing) Then moCallBack.AppDesc fRejectMsg
  564. End Sub
  565. Private Sub DirectPlay8Event_AsyncOpComplete(dpnotify As DxVBLibA.DPNMSG_ASYNC_OP_COMPLETE, fRejectMsg As Boolean)
  566.     'VB requires that we must implement *every* member of this interface
  567.     If (Not moCallBack Is Nothing) Then moCallBack.AsyncOpComplete dpnotify, fRejectMsg
  568. End Sub
  569. Private Sub DirectPlay8Event_ConnectComplete(dpnotify As DxVBLibA.DPNMSG_CONNECT_COMPLETE, fRejectMsg As Boolean)
  570.     Dim lMsg As Long, lOffset As Long
  571.     Dim oBuf() As Byte
  572.     If dpnotify.hResultCode = 0 Then 'Success!
  573.         cmdHangup.Enabled = True
  574.         'Now let's send a message asking the host to accept our call
  575.         lOffset = NewBuffer(oBuf)
  576.         lMsg = MsgAskToJoin
  577.         AddDataToBuffer oBuf, lMsg, LenB(lMsg), lOffset
  578.         dpp.SendTo DPNID_ALL_PLAYERS_GROUP, oBuf, 0, DPNSEND_NOLOOPBACK
  579.     Else
  580.         tmrUpdate.Enabled = True
  581.     End If
  582.     'VB requires that we must implement *every* member of this interface
  583.     If (Not moCallBack Is Nothing) Then moCallBack.ConnectComplete dpnotify, fRejectMsg
  584. End Sub
  585. Private Sub DirectPlay8Event_CreateGroup(ByVal lGroupID As Long, ByVal lOwnerID As Long, fRejectMsg As Boolean)
  586.     'VB requires that we must implement *every* member of this interface
  587.     If (Not moCallBack Is Nothing) Then moCallBack.CreateGroup lGroupID, lOwnerID, fRejectMsg
  588. End Sub
  589. Private Sub DirectPlay8Event_CreatePlayer(ByVal lPlayerID As Long, fRejectMsg As Boolean)
  590.     Dim dpPeer As DPN_PLAYER_INFO
  591.     On Error Resume Next
  592.     dpPeer = dpp.GetPeerInfo(lPlayerID)
  593.     If (dpPeer.lPlayerFlags And DPNPLAYER_LOCAL) = DPNPLAYER_LOCAL Then
  594.         glMyPlayerID = lPlayerID
  595.         lstUsers.ItemData(0) = glMyPlayerID
  596.     End If
  597.     'VB requires that we must implement *every* member of this interface
  598.     If (Not moCallBack Is Nothing) Then moCallBack.CreatePlayer lPlayerID, fRejectMsg
  599. End Sub
  600. Private Sub DirectPlay8Event_DestroyGroup(ByVal lGroupID As Long, ByVal lReason As Long, fRejectMsg As Boolean)
  601.     'VB requires that we must implement *every* member of this interface
  602.     If (Not moCallBack Is Nothing) Then moCallBack.DestroyGroup lGroupID, lReason, fRejectMsg
  603. End Sub
  604. Private Sub DirectPlay8Event_DestroyPlayer(ByVal lPlayerID As Long, ByVal lReason As Long, fRejectMsg As Boolean)
  605.     Dim dpPeer As DPN_PLAYER_INFO
  606.     On Error Resume Next
  607.     If lPlayerID <> glMyPlayerID Then 'ignore removing myself
  608.         RemovePlayer lPlayerID
  609.     End If
  610.     If Not (ChatWindow Is Nothing) Then Set moCallBack = ChatWindow 'If the chat window is open, let them know about the departure.
  611.     'VB requires that we must implement *every* member of this interface
  612.     If (Not moCallBack Is Nothing) Then moCallBack.DestroyPlayer lPlayerID, lReason, fRejectMsg
  613. End Sub
  614. Private Sub DirectPlay8Event_EnumHostsQuery(dpnotify As DxVBLibA.DPNMSG_ENUM_HOSTS_QUERY, fRejectMsg As Boolean)
  615.     'VB requires that we must implement *every* member of this interface
  616.     If (Not moCallBack Is Nothing) Then moCallBack.EnumHostsQuery dpnotify, fRejectMsg
  617. End Sub
  618. Private Sub DirectPlay8Event_EnumHostsResponse(dpnotify As DxVBLibA.DPNMSG_ENUM_HOSTS_RESPONSE, fRejectMsg As Boolean)
  619.     'VB requires that we must implement *every* member of this interface
  620.     If (Not moCallBack Is Nothing) Then moCallBack.EnumHostsResponse dpnotify, fRejectMsg
  621. End Sub
  622. Private Sub DirectPlay8Event_HostMigrate(ByVal lNewHostID As Long, fRejectMsg As Boolean)
  623.     'VB requires that we must implement *every* member of this interface
  624.     If (Not moCallBack Is Nothing) Then moCallBack.HostMigrate lNewHostID, fRejectMsg
  625. End Sub
  626. Private Sub DirectPlay8Event_IndicateConnect(dpnotify As DxVBLibA.DPNMSG_INDICATE_CONNECT, fRejectMsg As Boolean)
  627.     'VB requires that we must implement *every* member of this interface
  628.     If (Not moCallBack Is Nothing) Then moCallBack.IndicateConnect dpnotify, fRejectMsg
  629. End Sub
  630. Private Sub DirectPlay8Event_IndicatedConnectAborted(fRejectMsg As Boolean)
  631.     'VB requires that we must implement *every* member of this interface
  632.     If (Not moCallBack Is Nothing) Then moCallBack.IndicatedConnectAborted fRejectMsg
  633. End Sub
  634. Private Sub DirectPlay8Event_InfoNotify(ByVal lMsgID As Long, ByVal lNotifyID As Long, fRejectMsg As Boolean)
  635.     'VB requires that we must implement *every* member of this interface
  636.     If (Not moCallBack Is Nothing) Then moCallBack.InfoNotify lMsgID, lNotifyID, fRejectMsg
  637. End Sub
  638. Private Sub DirectPlay8Event_Receive(dpnotify As DxVBLibA.DPNMSG_RECEIVE, fRejectMsg As Boolean)
  639.     Dim lNewMsg As Long, lNewOffSet As Long
  640.     Dim oBuf() As Byte, f As frmProgress
  641.     Dim lMsg As Long, lOffset As Long
  642.     Dim frmJoin As frmJoinRequest
  643.     Dim dpPeer As DPN_PLAYER_INFO
  644.     Dim sFile As String, lUnique As Long
  645.     Dim oFile() As Byte, lFileSize As Long
  646.     Dim lChunkSize As Long, oData() As Byte
  647.     With dpnotify
  648.     GetDataFromBuffer .ReceivedData, lMsg, LenB(lMsg), lOffset
  649.     Select Case lMsg
  650.     Case MsgChat, MsgWhisper 'Make sure chat messages get to the chat window
  651.         If ChatWindow Is Nothing Then
  652.             Set ChatWindow = New frmChat
  653.         End If
  654.         ChatWindow.Show
  655.         Set moCallBack = ChatWindow
  656.     Case MsgSendDrawPixel, MsgClearWhiteBoard
  657.         If WhiteBoardWindow Is Nothing Then
  658.             Set WhiteBoardWindow = New frmWhiteBoard
  659.         End If
  660.         WhiteBoardWindow.Show
  661.         Set moCallBack = WhiteBoardWindow
  662.     Case MsgAskToJoin
  663.         If gfHost Then
  664.             'We are the host, pop up the 'Ask to join dialog
  665.             dpPeer = dpp.GetPeerInfo(dpnotify.idSender)
  666.             Set frmJoin = New frmJoinRequest
  667.             frmJoin.SetupRequest Me, dpnotify.idSender, dpPeer.Name
  668.             frmJoin.Show vbModeless
  669.         End If
  670.     Case MsgAcceptJoin
  671.         'We have been accepted
  672.         'Enumerate all the players and add anyone we don't already have listed
  673.         UpdatePlayerList
  674.         If Not (ChatWindow Is Nothing) Then ChatWindow.LoadAllPlayers
  675.         ConnectVoice Me
  676.     Case MsgRejectJoin
  677.         'We have been rejected
  678.         tmrJoin.Enabled = True
  679.         'We need to use a timer here, without it, we would be attempting to cleanup
  680.         'our dplay objects to restart our host before this message was done being processed.
  681.     Case MsgShowChat
  682.         'Someone wants to chat.  Open the chat window
  683.         If ChatWindow Is Nothing Then Set ChatWindow = New frmChat
  684.         ChatWindow.Show vbModeless
  685.         Set moCallBack = ChatWindow
  686.     Case MsgShowWhiteBoard
  687.         'Someone wants to draw.  Open the whiteboard window
  688.         If WhiteBoardWindow Is Nothing Then Set WhiteBoardWindow = New frmWhiteBoard
  689.         WhiteBoardWindow.Show vbModeless
  690.         Set moCallBack = WhiteBoardWindow
  691.     Case MsgSendFileRequest
  692.         'Someone wants to send us a file.  Should we accept?
  693.         dpPeer = dpp.GetPeerInfo(dpnotify.idSender)
  694.         GetDataFromBuffer .ReceivedData, lUnique, LenB(lUnique), lOffset
  695.         sFile = GetStringFromBuffer(.ReceivedData, lOffset)
  696.         LockReceiveCollection
  697.         Set f = New frmProgress
  698.         With f
  699.             .FileUniqueID = lUnique
  700.             .sFileName = sFile
  701.             .lDPlayID = dpnotify.idSender
  702.             Set .RequestForm = New frmTransferRequest
  703.             .RequestForm.SetupRequest Me, dpPeer.Name, .sFileName, .FileUniqueID, dpnotify.idSender
  704.             .RequestForm.Show vbModeless
  705.         End With
  706.         moReceivedFiles.Add f
  707.         UnlockReceiveCollection
  708.     Case MsgSendFileDeny
  709.         'We don't care about this file
  710.         GetDataFromBuffer .ReceivedData, lUnique, LenB(lUnique), lOffset
  711.         'Now remove this one
  712.         EraseSendFile lUnique
  713.     Case MsgSendFileAccept
  714.         'Ok, they do want us to send the file to them.. We will send it in chunks
  715.         'First we will send the file info
  716.         GetDataFromBuffer .ReceivedData, lUnique, LenB(lUnique), lOffset
  717.         'First we need to find the correct file in our send list
  718.         LockSendCollection
  719.         Set f = GetSendProgressForm(lUnique)
  720.         lNewOffSet = NewBuffer(oBuf)
  721.         lMsg = MsgSendFileInfo
  722.         AddDataToBuffer oBuf, lMsg, LenB(lMsg), lNewOffSet
  723.         With f
  724.             .lFileSize = FileLen(.sFileName)
  725.             AddDataToBuffer oBuf, .FileUniqueID, SIZE_LONG, lNewOffSet
  726.             AddDataToBuffer oBuf, .lFileSize, SIZE_LONG, lNewOffSet
  727.             dpp.SendTo .lDPlayID, oBuf, 0, DPNSEND_NOLOOPBACK Or DPNSEND_GUARANTEED
  728.             .SetFile .sFileName
  729.             .SetMax .lFileSize
  730.             .SetValue 0
  731.             .Show
  732.         End With
  733.         UnlockSendCollection
  734.     Case MsgSendFileInfo
  735.         'They just send us the file size, save it
  736.         GetDataFromBuffer .ReceivedData, lUnique, LenB(lUnique), lOffset
  737.         'First we need to find the correct file in our receive list
  738.         LockReceiveCollection
  739.         Set f = GetReceiveProgressForm(lUnique)
  740.         With f
  741.             GetDataFromBuffer dpnotify.ReceivedData, lFileSize, LenB(lFileSize), lOffset
  742.             .lFileSize = lFileSize
  743.             .SetFile .sFileName, True
  744.             .SetMax .lFileSize
  745.             .SetValue 0
  746.             .Show
  747.         End With
  748.         'Acknowledge that we received this part
  749.         lNewMsg = MsgAckFilePart
  750.         lNewOffSet = NewBuffer(oBuf)
  751.         AddDataToBuffer oBuf, lNewMsg, LenB(lNewMsg), lNewOffSet
  752.         AddDataToBuffer oBuf, lUnique, LenB(lUnique), lNewOffSet
  753.         dpp.SendTo dpnotify.idSender, oBuf, 0, DPNSEND_NOLOOPBACK Or DPNSEND_GUARANTEED
  754.         UnlockReceiveCollection
  755.     Case MsgSendFilePart
  756.         'They just send us the file size, save it
  757.         GetDataFromBuffer .ReceivedData, lUnique, LenB(lUnique), lOffset
  758.         GetDataFromBuffer .ReceivedData, lChunkSize, LenB(lChunkSize), lOffset
  759.         'First we need to find the correct file in our receive list
  760.         LockReceiveCollection
  761.         Set f = GetReceiveProgressForm(lUnique)
  762.         With f
  763.             ReDim oData(1 To lChunkSize)
  764.             'We just received a file part..  Append this to our current file
  765.             If .filNumber = 0 Then
  766.                 .filNumber = FreeFile
  767.                 If Dir$(App.Path & "\" & .sFileName) <> vbNullString Then Kill App.Path & "\" & .sFileName
  768.                 Open App.Path & "\" & .sFileName For Binary Access Write As #.filNumber
  769.             End If
  770.             GetDataFromBuffer dpnotify.ReceivedData, oData(1), lChunkSize, lOffset
  771.             Put #.filNumber, , oData
  772.             'Is this the end of the file?
  773.             .lCurrentPos = .lCurrentPos + lChunkSize
  774.             .SetValue .lCurrentPos
  775.             If .lCurrentPos >= .lFileSize Then
  776.                 'We're done with the file
  777.                 Close #.filNumber
  778.                 EraseReceiveFile .FileUniqueID
  779.             Else
  780.                 'Acknowledge that we received this part
  781.                 lNewMsg = MsgAckFilePart
  782.                 lNewOffSet = NewBuffer(oBuf)
  783.                 AddDataToBuffer oBuf, lNewMsg, LenB(lNewMsg), lNewOffSet
  784.                 AddDataToBuffer oBuf, lUnique, LenB(lUnique), lNewOffSet
  785.                 dpp.SendTo dpnotify.idSender, oBuf, 0, DPNSEND_NOLOOPBACK Or DPNSEND_GUARANTEED
  786.             End If
  787.         End With
  788.         UnlockReceiveCollection
  789.     Case MsgAckFilePart
  790.         GetDataFromBuffer .ReceivedData, lUnique, LenB(lUnique), lOffset
  791.         SendNextFilePart lUnique
  792.     Case MsgNewPlayerJoined
  793.         UpdatePlayerList 'Update our list here
  794.         If Not (ChatWindow Is Nothing) Then ChatWindow.LoadAllPlayers 'And in the chat window if we need to
  795.     End Select
  796.     End With
  797.     If (Not moCallBack Is Nothing) Then moCallBack.Receive dpnotify, fRejectMsg
  798. End Sub
  799. Private Sub DirectPlay8Event_SendComplete(dpnotify As DxVBLibA.DPNMSG_SEND_COMPLETE, fRejectMsg As Boolean)
  800.     'VB requires that we must implement *every* member of this interface
  801.     If (Not moCallBack Is Nothing) Then moCallBack.SendComplete dpnotify, fRejectMsg
  802. End Sub
  803. Private Sub DirectPlay8Event_TerminateSession(dpnotify As DxVBLibA.DPNMSG_TERMINATE_SESSION, fRejectMsg As Boolean)
  804.     'VB requires that we must implement *every* member of this interface
  805.     If (Not moCallBack Is Nothing) Then moCallBack.TerminateSession dpnotify, fRejectMsg
  806.     mfTerminate = True
  807.     tmrUpdate.Enabled = True
  808. End Sub
  809. Private Sub DirectPlayVoiceEvent8_ConnectResult(ByVal ResultCode As Long)
  810.     Dim lTargets(0) As Long
  811.     lTargets(0) = DVID_ALLPLAYERS
  812.     On Error Resume Next
  813.     'Connect the client
  814.     dvClient.SetTransmitTargets lTargets, 0
  815.     If Err.Number <> 0 And Err.Number <> DVERR_PENDING Then
  816.         mlVoiceError = Err.Number
  817.         tmrVoice.Enabled = True
  818.         Exit Sub
  819.     End If
  820. End Sub
  821. Private Sub DirectPlayVoiceEvent8_CreateVoicePlayer(ByVal playerID As Long, ByVal flags As Long)
  822.     'VB requires that we must implement *every* member of this interface
  823. End Sub
  824. Private Sub DirectPlayVoiceEvent8_DeleteVoicePlayer(ByVal playerID As Long)
  825.     'VB requires that we must implement *every* member of this interface
  826. End Sub
  827. Private Sub DirectPlayVoiceEvent8_DisconnectResult(ByVal ResultCode As Long)
  828.     'VB requires that we must implement *every* member of this interface
  829. End Sub
  830. Private Sub DirectPlayVoiceEvent8_HostMigrated(ByVal NewHostID As Long, ByVal NewServer As DxVBLibA.DirectPlayVoiceServer8)
  831.     'VB requires that we must implement *every* member of this interface
  832. End Sub
  833. Private Sub DirectPlayVoiceEvent8_InputLevel(ByVal PeakLevel As Long, ByVal RecordVolume As Long)
  834.     'VB requires that we must implement *every* member of this interface
  835. End Sub
  836. Private Sub DirectPlayVoiceEvent8_OutputLevel(ByVal PeakLevel As Long, ByVal OutputVolume As Long)
  837.     'VB requires that we must implement *every* member of this interface
  838. End Sub
  839. Private Sub DirectPlayVoiceEvent8_PlayerOutputLevel(ByVal SourcePlayerID As Long, ByVal PeakLevel As Long)
  840.     'VB requires that we must implement *every* member of this interface
  841. End Sub
  842. Private Sub DirectPlayVoiceEvent8_PlayerVoiceStart(ByVal SourcePlayerID As Long)
  843.     'VB requires that we must implement *every* member of this interface
  844. End Sub
  845. Private Sub DirectPlayVoiceEvent8_PlayerVoiceStop(ByVal SourcePlayerID As Long)
  846.     'VB requires that we must implement *every* member of this interface
  847. End Sub
  848. Private Sub DirectPlayVoiceEvent8_RecordStart(ByVal PeakVolume As Long)
  849.     'VB requires that we must implement *every* member of this interface
  850. End Sub
  851. Private Sub DirectPlayVoiceEvent8_RecordStop(ByVal PeakVolume As Long)
  852.     'VB requires that we must implement *every* member of this interface
  853. End Sub
  854. Private Sub DirectPlayVoiceEvent8_SessionLost(ByVal ResultCode As Long)
  855.     'VB requires that we must implement *every* member of this interface
  856. End Sub
  857.